/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package desarrollosoft.ghce.soporte;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

/**
 *
 * @author ariel
 */
public class ManejadorAutenticacionCorrecta implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authenticacion)
            throws IOException, ServletException {

        Collection<? extends GrantedAuthority> auths = authenticacion.getAuthorities();
        List<String> listaAutoridades = new ArrayList<>();
        
        for (GrantedAuthority authorities : auths) {
            listaAutoridades.add(authorities.getAuthority());
        }
        
        if (listaAutoridades.contains("ROLE_USUARIO_PROFESIONAL")) {
            // es medico
            response.sendRedirect("seguro/Principal.zul");
        } else if (listaAutoridades.contains("ROLE_USUARIO_ADMINISTRADOR")) {
            // es administrador
            response.sendRedirect("admin/PrincipalAdministrador.zul");
        } else if (listaAutoridades.contains("ROLE_USUARIO_RESETEADO")) {// sino si tiene role reseteado mandar a...
            response.sendRedirect("reset/CambiarContrasenia.zul");
        }
    }

}
